package com.geaosu.filesselector;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * 辖区概览
 */
public class HomeFragmentAdapter extends BaseAdapter {
    private List<FileBean> sList = new ArrayList<>();

    public void replaceAll(List<FileBean> list) {
        sList.clear();
        sList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return sList.size();
    }

    @Override
    public Object getItem(int position) {
        return sList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = View.inflate(parent.getContext(), R.layout.fragment_home_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        FileBean bean = sList.get(position);
        String name = bean.getName();
        viewHolder.tvTitle.setText(name);
        switch (name) {
            case "全部文件":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_folder);
                break;
            case "最近使用":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_download);
                break;
            case "下载":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_download);
                break;
            case "图片":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_img);
                break;
            case "音乐":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_mp3);
                break;
            case "视频":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_video);
                break;
            case "文稿":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_word);
                break;
            case "安装包":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_folder);
                break;
            case "压缩包":
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_zip);
                break;
            default:
                viewHolder.ivIcon.setBackgroundResource(R.mipmap.extension_err);
                break;
        }
        return convertView;
    }

    public static class ViewHolder {
        private ImageView ivIcon;
        private TextView tvTitle;

        public ViewHolder(View view) {
            this.ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
            this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        }
    }
}
